//
//  FXCPianoViewController.m
//  Various
//
//  Created by 范新晨 on 16/3/23.
//  Copyright © 2016年 Harword. All rights reserved.
//

#import "FXCPianoViewController.h"
#import "HLNetWorkTool.h"
#import "UIView+Frame.h"
#import "FXCPianoModel.h"
#import "FXCCenterCollectionItem.h"
#import "FXCBottomCollectionItem.h"
#import <MJExtension.h>
#import "UIColor+convert.h"
#import "FXCBottomCollectionView.h"
#import "UITabBarController+HLHidden.h"
#import <Masonry.h>
#import "FXCDetailViewController.h"

@interface FXCPianoViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, FXCBottomCollectionViewDelegate>

@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UICollectionView *centerCollection;
@property (nonatomic, strong) FXCBottomCollectionView *bottomCollection;

@property (nonatomic, strong) NSMutableArray *arrModel;

@property (nonatomic, assign) int index;
@property (nonatomic, strong) NSIndexPath *lastIndex;

@property (nonatomic, strong) NSMutableArray *cellArr;


@end

@implementation FXCPianoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //    self.tabBarController.tabBar.hidden = YES;
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor cyanColor];
    self.navigationController.navigationBarHidden = YES;
    
    [UIApplication sharedApplication].statusBarStyle =UIStatusBarStyleLightContent;
    
    self.index = 0;
    self.arrModel = [NSMutableArray array];
    self.cellArr = [NSMutableArray array];
    
    NSString *url = @"http://zuimeia.com/api/apps/app/daily/?&appVersion=2.2.4&openUDID=1bf9ccab8d121135bed763089b514aff901ffc28&resolution=%257B750%252C%25201334%257D&systemVersion=9.1&page_size=10&platform=1&page=1";
    [HLNetWorkTool getWithURL:url Parameter:nil HttpHeader:nil ResponseType:ResponseTypeJSON Progress:nil Success:^(id result) {
        
        NSDictionary *dic = result[@"data"];
        NSArray *arr = dic[@"apps"];
        //        NSLog(@"%ld", arr.count);
        
        //        self.arrModel = [NSMutableArray arrayWithArray:arr];
        for (NSDictionary *dic in arr) {
            FXCPianoModel *model = [FXCPianoModel mj_objectWithKeyValues:dic];
            [self.arrModel addObject:model];
        }
        
        
        
        [self.centerCollection reloadData];
        [self.bottomCollection reloadData];
        
        //        第一个item的y坐标是15
        self.index = 0;
        [self.bottomCollection setContentOffset:CGPointZero animated:YES];
        [self scrollViewDidEndDecelerating:self.bottomCollection];
        
    } Failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    
    //    self.view.backgroundColor = [UIColor colorWithHexString:self.arrModel[self.index]];
    
    
    UICollectionViewFlowLayout *centerLayout = [[UICollectionViewFlowLayout alloc] init];
    centerLayout.itemSize = CGSizeMake(FXCWEIGHT - 10, FXCHEIGHT / 7 * 5);
    centerLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    centerLayout.sectionInset = UIEdgeInsetsMake(0, 5, 0, 5);
    self.centerCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 60, FXCWEIGHT, FXCHEIGHT / 7 * 5) collectionViewLayout:centerLayout];
    self.centerCollection.showsHorizontalScrollIndicator = NO;
    self.centerCollection.pagingEnabled = YES;
    [self.centerCollection registerClass:[FXCCenterCollectionItem class] forCellWithReuseIdentifier:@"centerCollection"];
    self.centerCollection.backgroundColor = [UIColor whiteColor];
    self.centerCollection.tag = 100;
    self.centerCollection.delegate = self;
    self.centerCollection.dataSource = self;
    [self.view addSubview:self.centerCollection];
    
    
    UICollectionViewFlowLayout *bottomLayout = [[UICollectionViewFlowLayout alloc] init];
    bottomLayout.itemSize = CGSizeMake(FXCWEIGHT / 8 , 50);
    bottomLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    bottomLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    bottomLayout.minimumLineSpacing = 0;
    self.bottomCollection = [[FXCBottomCollectionView alloc] initWithFrame:CGRectMake(0, FXCHEIGHT - 60 - 49, FXCWEIGHT, FXCHEIGHT / 7) collectionViewLayout:bottomLayout];
    self.bottomCollection.backgroundColor = [UIColor redColor];
    self.bottomCollection.scrollEnabled = NO;
    self.bottomCollection.showsVerticalScrollIndicator = NO;
    self.bottomCollection.tag = 101;
    self.bottomCollection.delegate = self;
    self.bottomCollection.dataSource = self;
    [self.bottomCollection registerClass:[FXCBottomCollectionItem class] forCellWithReuseIdentifier:@"bottomCollection"];
    [self.view addSubview:self.bottomCollection];

    self.bottomCollection.backgroundColor = [UIColor cyanColor];
    self.centerCollection.backgroundColor = [UIColor cyanColor];
    
    self.bottomCollection.bottomDelegate = self;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(back) name:@"back" object:nil];
    
}
- (void)back{
    [self.centerCollection setContentOffset:CGPointZero animated:YES];
    [self.bottomCollection setContentOffset:CGPointZero animated:YES];
    
//    self.index = 0;
    UICollectionViewCell *cell = [self.bottomCollection cellForItemAtIndexPath:[NSIndexPath indexPathForRow:self.index inSection:0]];
    cell.y = 50;
    
    [self bottomAnimation:[NSIndexPath indexPathForRow:0 inSection:0]];
    
    
}
- (void)bottomCollectionViewChangeCenterView:(NSIndexPath *)indexPath{
    [self.centerCollection scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
    
    [UIView animateWithDuration:0.5 animations:^{
        if (self.index) {
            FXCPianoModel *model = self.arrModel[self.index];
            self.view.backgroundColor = [UIColor colorWithHexString:model.recommanded_background_color];
            self.bottomCollection.backgroundColor = [UIColor colorWithHexString:model.recommanded_background_color];
            self.centerCollection.backgroundColor = [UIColor colorWithHexString:model.recommanded_background_color];
        }
    }];
    
    
    if (self.lastIndex.row == indexPath.row) {
        return;
    }
    
    UICollectionViewCell *cell = [self.bottomCollection cellForItemAtIndexPath:indexPath];
    
    
    if (cell == nil) {
        [self.bottomCollection layoutIfNeeded];
        cell = [self.bottomCollection cellForItemAtIndexPath:indexPath];
        
    }
    
    if (cell) {
        if (cell.x < FXCWEIGHT *0.6) {
            [self.bottomCollection setContentOffset:CGPointZero animated:YES];
        } else {
            
            if (self.lastIndex.row < indexPath.row) {
                [self.bottomCollection setContentOffset:CGPointMake(self.bottomCollection.contentOffset.x + cell.width , 0) animated:YES];
            } else {
                [self.bottomCollection setContentOffset:CGPointMake(self.bottomCollection.contentOffset.x - cell.width , 0) animated:YES];
            }
        }
    }
    self.lastIndex = indexPath;
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.tag == 100) {
        int index = (scrollView.contentOffset.x + scrollView.width * 0.5) / scrollView.width;
        self.index = index;
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    [self bottomAnimation:[NSIndexPath indexPathForRow:self.index inSection:0]];
}

- (void)bottomAnimation:(NSIndexPath *)indexPath{
    [UIView animateWithDuration:0.5 animations:^{
        if (self.index) {
            FXCPianoModel *model = self.arrModel[self.index];
            self.view.backgroundColor = [UIColor colorWithHexString:model.recommanded_background_color];
            self.bottomCollection.backgroundColor = [UIColor colorWithHexString:model.recommanded_background_color];
            self.centerCollection.backgroundColor = [UIColor colorWithHexString:model.recommanded_background_color];
        }
    }];
    
    
    if (self.lastIndex.row == indexPath.row) {
        return;
    }
    
    UICollectionViewCell *cell = [self.bottomCollection cellForItemAtIndexPath:indexPath];
    
    
    if (cell == nil) {
        [self.bottomCollection layoutIfNeeded];
        cell = [self.bottomCollection cellForItemAtIndexPath:indexPath];
        
    }
    
    
    
    if (cell) {
        if (cell.x < FXCWEIGHT *0.6) {
            [self.bottomCollection setContentOffset:CGPointZero animated:YES];
        } else {
            
            if (self.lastIndex.row < indexPath.row) {
                [self.bottomCollection setContentOffset:CGPointMake(self.bottomCollection.contentOffset.x + cell.width , 0) animated:YES];
            } else {
                [self.bottomCollection setContentOffset:CGPointMake(self.bottomCollection.contentOffset.x - cell.width , 0) animated:YES];
            }
        }
        
        [UIView animateWithDuration:0.3 animations:^{
            cell.y = 10;
        } completion:^(BOOL finished) {
            cell.y = 15;
        }];
        
        UICollectionViewCell *lastCell = [self.bottomCollection cellForItemAtIndexPath:self.lastIndex];
        
        [UIView animateWithDuration:0.3 animations:^{
            lastCell.y = 60;
        } completion:^(BOOL finished) {
            lastCell.y = 50;
        }];
        
        
    }
    
    self.lastIndex = indexPath;
}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.arrModel.count;
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    FXCPianoModel *model = self.arrModel[indexPath.row];
    if (collectionView.tag == 100) {
        FXCCenterCollectionItem *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"centerCollection" forIndexPath:indexPath];
        cell.model = model;
        
        
        return cell;
    } else {
        FXCBottomCollectionItem *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"bottomCollection" forIndexPath:indexPath];
        cell.y = 50;
        cell.model = model;
        return cell;
    }
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView.tag == 100) {
        FXCDetailViewController *detailVC = [[FXCDetailViewController alloc] init];
        FXCPianoModel *model = self.arrModel[indexPath.row];
        detailVC.model = model;
        [self.navigationController pushViewController:detailVC animated:YES];
        
    }
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
